package com.example.his.api.mis.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import com.example.his.api.common.PageUtils;
import com.example.his.api.db.dao.UserDao;
import com.example.his.api.db.pojo.UserEntiy;
import com.example.his.api.mis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
  
  //  @Autowired
  @Resource
  private UserDao userDao;
  
  @Override
  public Integer login(Map param) {
    String username = MapUtil.getStr(param, "username");
    String password = MapUtil.getStr(param, "password");
    MD5 md5 = MD5.create();
    String temp = md5.digestHex(username);
    String tempStart = StrUtil.subWithLength(temp, 0, 6);
    String tempEnd = StrUtil.subSuf(temp, temp.length() - 3);
    password = md5.digestHex(tempStart + password + tempEnd).toUpperCase();
    param.replace("password", password);
    Integer userId = userDao.login(param);
    return userId;
  }
  
  @Override
  public int updatePassword(Map param) {
    int userId = MapUtil.getInt(param, "userId");
    String username = userDao.searchUsernameById(userId);
    String password = MapUtil.getStr(param, "password");
    
    MD5 md5 = MD5.create();
    String temp = md5.digestHex(username);
    String tempStart = StrUtil.subWithLength(temp, 0, 6);
    String tempEnd = StrUtil.subSuf(temp, temp.length() - 3);
    password = md5.digestHex(tempStart + password + tempEnd).toUpperCase();
    param.replace("password", password);
    String newPassword = MapUtil.getStr(param, "newPassword");
    newPassword = md5.digestHex(tempStart + newPassword + tempEnd).toUpperCase();
    param.replace("newPassword", newPassword);
    
    int rows = userDao.updatePassword(param);
    return rows;
  }
  
  @Override
  public PageUtils searchByPage(Map param) {
    //    让返回给前端的数据中是空数组而不是null
    ArrayList <HashMap> list = new ArrayList <>();
    long count = userDao.searchCount(param);
    if(count > 0) {
      list = userDao.searchByPage(param);
    }
    
    int page = MapUtil.getInt(param, "page");
    int length = MapUtil.getInt(param, "length");
    PageUtils pageUtils = new PageUtils(list, count, page, length);
    return pageUtils;
  }
  
  @Override
  @Transactional
  public int insert(UserEntiy user) {
    MD5 md5 = MD5.create();
    String temp = md5.digestHex(user.getUsername());
    String tempStart = StrUtil.subWithLength(temp, 0, 6);
    String tempEnd = StrUtil.subSuf(temp, temp.length() - 1);
    
    String password = md5.digestHex(tempStart + user.getPassword() + tempEnd).toUpperCase();
    user.setPassword(password);
    int rows = userDao.insert(user);
    return rows;
  }
  
  @Override
  public HashMap searchById(int userId) {
    HashMap map = userDao.searchById(userId);
    return map;
  }
  
  @Override
  @Transactional
  public int update(Map param) {
    int rows = userDao.update(param);
    return rows;
  }
  
  @Override
  @Transactional
  public int deleteByIds(Integer[] ids) {
    int rows = userDao.deleteByIds(ids);
    return rows;
  }
  
  @Override
  @Transactional
  public int dismiss(int userId) {
    int rows = userDao.dismiss(userId);
    return rows;
  }
  
  @Override
  public HashMap searchDoctorById(int id) {
    HashMap map = userDao.searchDoctorById(id);
    return map;
  }
  
  @Override
  public PageUtils searchByDeptId(Map param) {
    ArrayList list = new ArrayList <>();
    long count = userDao.searchCountByDeptId(param);
    if(count > 0) {
      list = userDao.searchByDeptId(param);
    }
    int page = MapUtil.getInt(param, "page");
    int length = MapUtil.getInt(param, "length");
    PageUtils pageUtils = new PageUtils(list, count, page, length);
    
    return pageUtils;
  }
  
  
}
